const helper = new Helper(this);
// 下拉框form 表单
const search = helper.get('search')?.widgetObj
const eventTitle = helper.get('eventTitle')?.widgetObj
const list = helper.get('list')?.widgetObj
const prefix = helper.getVariable('environmentType');
const videoPlatform = helper.getVariable('videoPlatform');
// 基础服务
const basicRequest = BasicServices.getInstance(prefix);
// 应急服务
const emergencyRequest = EmergencyServices.getInstance(prefix);
// 页面路由参数
let eventParams = helper.parseVariable("screen_params")

if (eventParams) {
    // 渲染标题
    eventTitle._option.links.text = eventParams.eventTitle
    eventTitle.render()
    // 设置事件数据
    list.setEvent(eventParams)

    // 请求事件级别列表
    basicRequest.getEventLevelList({}).then(({ rows }) => {
        console.log('请求事件级别列表: => ', rows)
        if (Array.isArray(rows)) {
            // 找到当前事件对应目标级别
            const curLevel = rows?.find(v => v.id === eventParams.eventGradeCode)?.eventLevelCode
            const params = {
                eventType: eventParams.eventTypeCode,
                eventLevel: curLevel,
            }
            // 获取当前事件对应推荐预案
            emergencyRequest.getEmergencyPlan(params).then(result => {
                console.log('获取当前事件对应推荐预案: => ', result)
                if (Array.isArray(result)) {
                    // 找到第一个推荐的
                    let first;
                    const arr = result.map((v, index) => {
                        v.value = v.id;
                        if (index === 0) {
                            v.name = v.planName + '(推荐)';
                            first = v;
                        }

                        return v
                    })

                    // 为表单赋值
                    search.asyncSelectOptions('name', arr)

                    if (first) {
                        search.setFormData('form_1', {
                            name: first.id,
                            level: curLevel
                        })

                        // 设置预案列表的搜索条件
                        list.setParams(first.id, curLevel)
                    }
                    list.initQuery()
                }
            })
        }
    })


}
